package com.uchedao.seller.module;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.util.ToastUtil;

/**
 * 基础信息
 *
 * @author "叶晋"
 */
public class BaseInfo implements Cloneable {
    @Expose
    @SerializedName("process_info")
    public ProcessInfo process_info = new ProcessInfo();// 手续信息

    @Expose
    @SerializedName("transfer_info")
    public TransferInfo transfer_info = new TransferInfo();// 过户情况

    @Expose
    @SerializedName("owner_info")
    public OwnerInfo owner_info = new OwnerInfo();// 买主信息

    @Expose
    @SerializedName("use_info")
    public UseInfo use_info = new UseInfo();// 使用信息

    @Expose
    @SerializedName("car_type_info")
    public CarTypeInfo car_type_info = new CarTypeInfo();// 车型信息

    @Expose
    @SerializedName("trade_info")
    public TradeInfo trade_info = new TradeInfo();// 交易信息

    @Expose
    @SerializedName("attach_info")
    public AttachInfo attach_info = new AttachInfo();// 附加信息

    @Expose
    @SerializedName("desc_info")
    public String desc_info = "";// 车况描述

    @Override
    public BaseInfo clone() {
        BaseInfo baseInfo = null;
        try {
            baseInfo = (BaseInfo) super.clone();
            baseInfo.process_info = process_info.clone();
            baseInfo.transfer_info = transfer_info.clone();
            baseInfo.owner_info = owner_info.clone();
            baseInfo.use_info = use_info.clone();
            baseInfo.car_type_info = car_type_info.clone();
            baseInfo.trade_info = trade_info.clone();
            baseInfo.attach_info = attach_info.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return baseInfo;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        BaseInfo baseInfo = (BaseInfo) obj;

        if (!process_info.equals(baseInfo.process_info))
            return false;
        if (!transfer_info.equals(baseInfo.transfer_info))
            return false;
        if (!owner_info.equals(baseInfo.owner_info))
            return false;
        if (!use_info.equals(baseInfo.use_info))
            return false;
        if (!car_type_info.equals(baseInfo.car_type_info))
            return false;
        if(!trade_info.equals(baseInfo.trade_info))
            return false;
        if(!attach_info.equals(baseInfo.attach_info))
            return false;
        if(!desc_info.equals(baseInfo.desc_info))
            return false;
        return true;
    }

    /**
     * 得到状态
     *
     * @return
     */
    public int getStatus() {
        return getStatus(false);
    }

    /**
     * 得到状态
     *
     * @return
     */
    public int getStatus(boolean showToast) {
        boolean hasEmpty = false;//基本信息-手续信息 有空项
        boolean hasData = false;// 有有数据项
        boolean hasEmptyCar = false;// 基本信息-车辆信息 有空项

        String message = "";// 提示信息文字
        String[] process_info_datas = {process_info.save_price,process_info.invoice,
                process_info.driving_license, process_info.registration,
                process_info.surtax_card, process_info.travel_tax,
                process_info.year_check, process_info.safe_force,
                process_info.safe_business, process_info.owner_card,
                process_info.body_nameplate, process_info.illegal_info,};

        for (String s : process_info_datas) {
            if (TextUtils.isEmpty(s)) {
                hasEmpty = true;
                message = "【基本信息】-【手续信息】有项目为空";
            } else {
                hasData = true;
            }
        }

        // 车船税有效期
        if ("有".equals(process_info.travel_tax)
                && TextUtils.isEmpty(process_info.travel_tax_time)) {
            hasEmpty = true;
            message = "【基本信息】-【手续信息】-【车船税有效期】为空";
        }

        // 年检有效期
        if ("有效".equals(process_info.year_check)
                && TextUtils.isEmpty(process_info.year_check_time)) {
            hasEmpty = true;
            message = "【基本信息】-【手续信息】-【年检有效期】为空";
        }

        // 交强险有效期
        if ("有".equals(process_info.safe_force)
                && TextUtils.isEmpty(process_info.safe_force_time)) {
            hasEmpty = true;
            message = "【基本信息】-【手续信息】-【交强险有效期】为空";
        }

        // 商业保险
        if ("有".equals(process_info.safe_business)) {
            // 随车出售为空
            if (TextUtils.isEmpty(process_info.safe_business_sale)) {
                hasEmpty = true;
                message = "【基本信息】-【手续信息】-【随车出售】为空";

                // 随车出售
            } else if ("是".equals(process_info.safe_business_sale)) {
                // 金额、有效期、保险公司 任一为空
                if (TextUtils.isEmpty(process_info.safe_business_amount)) {
                    hasEmpty = true;
                    message = "【基本信息】-【手续信息】-【商业险金额】为空";

                } else if (TextUtils.isEmpty(process_info.safe_business_time)) {
                    hasEmpty = true;
                    message = "【基本信息】-【手续信息】-【商险有效期】为空";

                } else if (TextUtils.isEmpty(process_info.insurance_company)) {
                    hasEmpty = true;
                    message = "【基本信息】-【手续信息】-【保险公司】为空";

                } else if ("自填".equals(process_info.insurance_company)
                        && TextUtils
                        .isEmpty(process_info.insurance_company_other)) {
                    hasEmpty = true;
                    message = "【基本信息】-【手续信息】-【保险公司】为空";
                }
            }

        }

        // 违章
        if ("有".equals(process_info.illegal_info)) {
            // 罚款或积分为空
            if (TextUtils.isEmpty(process_info.illegal_fine)) {
                hasEmpty = true;
                message = "【基本信息】-【手续信息】-【违章罚款】为空";

            } else if (TextUtils.isEmpty(process_info.illegal_detained_scores)) {
                hasEmpty = true;
                message = "【基本信息】-【手续信息】-【违章扣分】为空";
            }
        }
        // 过户情况
        String[] transfer_info_datas = {transfer_info.transfer_appearance,
                transfer_info.transfer_engine,
                transfer_info.transfer_frame_number,
                transfer_info.transfer_mortgage, transfer_info.transfer_num,
                transfer_info.transfer_other};
        for (String s : transfer_info_datas) {
            if (TextUtils.isEmpty(s)) {
                hasEmpty = true;
                message = "【基本信息】-【过户情况】有项目为空";
            } else {
                hasData = true;
            }
        }

        GlobalInfo.draft.statusChildren.put("00", hasEmpty);

        // 非必填
        String[] owner_info_datas = {owner_info.owner_name,
                owner_info.cert_number, owner_info.owner_contact,
                owner_info.trustor_name, owner_info.trustor_cert_number,
                owner_info.trustor_contact};

        for (String s : owner_info_datas) {
            if (TextUtils.isEmpty(s)) {
                // hasEmpty = true;
            } else {
                hasData = true;
            }
        }

        String[] use_info_datas = {
                use_info.province,
                use_info.city,
                use_info.card_time,
                use_info.kilometer,
                use_info.color,
                use_info.emission_standards};

        for (String s : use_info_datas) {
            if (TextUtils.isEmpty(s)) {
                hasEmptyCar = true;
                message = "【基本信息】-【车辆使用信息】有项目为空";
            } else {
                hasData = true;
            }
        }

        //*********2015-9-23,车型筛选改为一体化，直接显示品牌+车系+车型
//		// 车型为空
//		if (!hasEmptyCar
//				&& TextUtils.isEmpty(use_info.model_str)
//				&& CarDbHelper.getCarModel(UCheDaoApplication.getContext(),
//				use_info.series_id).size() > 0) {
//			hasEmptyCar = true;
//			message = "【基本信息】-【车辆使用信息】-【车型】为空";
//		}
//
//		// 车型为手动输入，且自填车型为空时
//		if ("手动输入".equals(use_info.model_str)
//				&& TextUtils.isEmpty(use_info.model_custom)) {
//			hasEmptyCar = true;
//			message = "【基本信息】-【车辆使用信息】-【自填车型】为空";
//		}

        String[] car_type_info_datas = {car_type_info.import_type,
                car_type_info.car_engines, car_type_info.emission_standards,
                car_type_info.driver_number, car_type_info.trans_type,
                car_type_info.kilometer, car_type_info.drive_type};
        for (String s : car_type_info_datas) {
            if (TextUtils.isEmpty(s)) {
                hasEmptyCar = true;
                message = "【基本信息】-【车辆车型信息】有项目为空";
            } else {
                hasData = true;
            }
        }


        // 二维码ID
        if (TextUtils.isEmpty(car_type_info.getCheckid())) {
            hasEmptyCar = true;
            message = "【基本信息】-【车辆车型信息】-【二维码ID】为空";
        } else {
            hasData = true;
        }

        GlobalInfo.draft.statusChildren.put("01", hasEmptyCar);

        if (hasEmpty && hasData || hasEmptyCar) {
            if (showToast)
                ToastUtil.showToastLong(message);
            return 1;// 进行中
        } else if (hasData)
            return 2;// 已完成
        else {
            if (showToast)
                ToastUtil.showToastLong("【基本信息】尚未开始");
            return 0;// 未开始
        }
    }

    public String toString() {
        return new Gson().toJson(this);
    }
}
